//
//  FGListVoucherController.m
//  Mi6App
//
//  Created by Mr.Lemon on 7/25/13.
//  Copyright (c) 2013 4G Secure. All rights reserved.
//

#import "FGListVoucherController.h"

@interface FGListVoucherController ()

{
     NSArray *sampleURLs;
}
@end

@implementation FGListVoucherController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.landscapeModeView.frame = CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width);
    [self.view addSubview:self.landscapeModeView];
//    self.view = self.landscapeModeView;
    self.landscapeModeView.hidden = YES;
    
    //self.title = LocalizedString(@"List Voucher");

    
    
    [self loadData];
    [self.tblVoucher reloadData];
    // Do any additional setup after loading the view from its nib.
    [self bindDataToLandScapeView];
}
-(void)bindDataToLandScapeView{
     photoGalleryVC.dataSource = self;
    self.photoGallery.dataSource=self;
    self.photoGallery.delegate=self;
    self.photoGallery.initialIndex = 4;
    self.photoGallery.subviewGap = 30;
    self.photoGallery.showsScrollIndicator = NO;
    
    sampleURLs = @[
    @"http://l.yimg.com/g/images/bg_error_hold_your_clicks.jpg",
    @"http://farm9.staticflickr.com/8418/8782168922_c69e58dcd5_z.jpg",
    @"http://farm8.staticflickr.com/7407/8717876655_13bcca7b16_z.jpg",
    @"http://farm9.staticflickr.com/8127/8708655358_817632ca88_z.jpg",
    @"http://farm9.staticflickr.com/8258/8707530059_005b8d30ff_z.jpg",
    @"http://farm9.staticflickr.com/8137/8707528977_e9662f67b6_z.jpg",
    @"http://farm9.staticflickr.com/8117/8704889239_a3ba9a50e7_z.jpg",
    @"http://farm9.staticflickr.com/8280/8706003566_cf3207145a_z.jpg",
    @"http://farm9.staticflickr.com/8405/8704879333_6fe24e8675_z.jpg",
    @"http://farm9.staticflickr.com/8258/8705999562_6b39e981d9_z.jpg",
    @"http://farm9.staticflickr.com/8395/8705998628_4ab56a6746_z.jpg"
    ];
    
}
-(void)loadData{
    
    
    self.listVoucher=[[NSMutableArray alloc] init];
    
    for (NSInteger i=0; i<15; i++) {
        FGVoucherInfo *voucher=[[FGVoucherInfo alloc] initWithId:@"1" title:@"" desc:@"V" url_image:@"" andExpiredDate:@"23/3/2014"];
        [self.listVoucher addObject:voucher];
    }
    
    
   
}

#pragma mark - IOS6 rotate
- (BOOL)shouldAutorotate {
    return  YES;
}

#pragma mark - IOS5 rotate
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
        self.view = self.portraitModeView;
        self.landscapeModeView.hidden = YES;
        self.portraitModeView.hidden = NO;
    } else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
               toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        self.view = self.landscapeModeView;
        self.landscapeModeView.hidden = NO;
        self.portraitModeView.hidden = YES;
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return  YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setTblVoucher:nil];
    [self setPhotoGallery:nil];
    [super viewDidUnload];
}

#pragma mark Table View

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.listVoucher count];
//    return 10;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier =@"FGVoucherCell";
    FGVoucherCell *cell = (FGVoucherCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FGVoucherCell" owner:(id)[FGVoucherCell class] options:nil];
        cell = (FGVoucherCell *)[nib objectAtIndex:0];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundView.backgroundColor = [UIColor clearColor];
    }
    
    FGVoucherInfo *mObject=[self.listVoucher objectAtIndex:indexPath.row];
    
//    cell.lblExpiredDate.text=mObject.expiredDate;
//    cell.lblVoucherName.text=mObject.description;
    cell.lblExpiredDate.text=@"23/7/2013";
    cell.lblVoucherName.text=@"Name as";
    [cell.imgVoucher setImageWithURL:[NSURL URLWithString:@"https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcRPPyKoZxEBgx7DGhItuP_ZpsudVpRz6pqu3ZykVXM9WC9bJ9vrvw"] placeholderImage:nil];
    
    
    //Handle event on each cell 
    UITapGestureRecognizer *shareClickGesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onShareVoucherClick:)];
    [shareClickGesture setNumberOfTapsRequired:1];
    [[cell btnShare] setTag:indexPath.row];
    [[cell btnShare] addGestureRecognizer:shareClickGesture];
    
    UITapGestureRecognizer *mapClickGesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onMapVoucherClick:)];
    [shareClickGesture setNumberOfTapsRequired:1];
    [[cell btnMap] setTag:indexPath.row];
    [[cell btnMap] addGestureRecognizer:mapClickGesture];
    
    cell.btnMap.userInteractionEnabled = YES;
    cell.btnShare.userInteractionEnabled = YES;
   
    
    return cell;
}
-(void)onShareVoucherClick:(UITapGestureRecognizer*)sender{
    
    NSLog(@"Share Click with tag :%d", sender.view.tag);
}
-(void)onMapVoucherClick:(UITapGestureRecognizer*)sender{
    
    NSLog(@"Map Click with tag :%d", sender.view.tag);
    FGMapViewController *viewController=[[FGMapViewController alloc]  initWithNibName:@"FGMapViewController" bundle:nil];
    [self.navigationController pushViewController:viewController animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FGVoucherDetailViewController *viewControll=[[FGVoucherDetailViewController alloc] initWithNibName:@"FGVoucherDetailViewController" bundle:nil];
    
    [self.navigationController pushViewController:viewControll animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete)
    {
        [self.listVoucher removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}

 
#pragma mark UIPhotoGalleryDataSource methods

- (NSInteger)numberOfViewsInPhotoGallery:(UIPhotoGalleryView *)photoGallery {
    return 10;
}

- (UIImage*)photoGallery:(UIPhotoGalleryView*)photoGallery localImageAtIndex:(NSInteger)index {
    return [UIImage imageNamed:[NSString stringWithFormat:@"sample%d.jpg", index % 10]];
}

- (NSURL*)photoGallery:(UIPhotoGalleryView *)photoGallery remoteImageURLAtIndex:(NSInteger)index {
    return sampleURLs[index % 10];
}

- (UIView*)photoGallery:(UIPhotoGalleryView *)photoGallery customViewAtIndex:(NSInteger)index {
    CGRect frame = CGRectMake(0, 0, photoGallery.frame.size.width, photoGallery.frame.size.height);
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = [NSString stringWithFormat:@"%d", index+1];
    [view addSubview:label];
    
    return view;
}

- (NSString*)photoGallery:(UIPhotoGalleryView *)photoGallery plainTextCaptionAtIndex:(NSInteger)index {
    return sampleURLs[index % 10];
}

- (NSAttributedString*)photoGallery:(UIPhotoGalleryView *)photoGallery attributedTextCaptionAtIndex:(NSInteger)index {
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:sampleURLs[index]];
    [attributedText addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(3,5)];
    [attributedText addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(10,7)];
    [attributedText addAttribute:NSFontAttributeName
                           value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:20.0]
                           range:NSMakeRange(20, 10)];
    return attributedText;
}

- (UIView*)photoGallery:(UIPhotoGalleryView *)photoGallery customViewCaptionAtIndex:(NSInteger)index {
    UILabel *customView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
    customView.text = sampleURLs[index];
    
    return customView;
}

//- (UIView*)customTopViewForGalleryViewController:(UIPhotoGalleryViewController *)galleryViewController {
//    CGFloat width = MIN([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
//    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 40)];
//    topView.backgroundColor = [UIColor whiteColor];
//
//    UIButton *btnClose = [UIButton buttonWithType:UIButtonTypeInfoDark];
//    btnClose.frame = CGRectMake(width-30, 10, 20, 20);
//    btnClose.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
//    [btnClose setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//    [btnClose addTarget:self
//                 action:@selector(goBackFromGallery)
//       forControlEvents:UIControlEventTouchUpInside];
//    [topView addSubview:btnClose];
//
//    return topView;
//}

- (UIView*)customTopViewForGalleryViewController:(UIPhotoGalleryViewController *)galleryViewController {
    return nil;
}

//- (UIView*)customBottomViewForGalleryViewController:(UIPhotoGalleryViewController *)galleryViewController {
//    return nil;
//}


#pragma UIPhotoGalleryDelegate methods
- (void)photoGallery:(UIPhotoGalleryView *)photoGallery didTapAtIndex:(NSInteger)index {
}

- (UIPhotoGalleryDoubleTapHandler)photoGallery:(UIPhotoGalleryView *)photoGallery doubleTapHandlerAtIndex:(NSInteger)index {
    switch (photoGallery.galleryMode) {
        case UIPhotoGalleryModeImageLocal:
            return UIPhotoGalleryDoubleTapHandlerZoom;
            
        case UIPhotoGalleryModeImageRemote:
            return UIPhotoGalleryDoubleTapHandlerNone;
            
        default:
            return UIPhotoGalleryDoubleTapHandlerCustom;
    }
}

- (void)photoGallery:(UIPhotoGalleryView *)photoGallery didDoubleTapAtIndex:(NSInteger)index {
//    NSlog(@"invoke");
    DEBUGLog(@"Gallery clicked  %@",index);
    FGVoucherDetailViewController *viewController=[[FGVoucherDetailViewController alloc] initWithNibName:@"" bundle:nil];
    [self.navigationController pushViewController:viewController animated:YES];
    
    
}
- (IBAction)btnFullscreenPressed:(UIButton *)sender {
    if (!photoGalleryVC) {
        photoGalleryVC = [[UIPhotoGalleryViewController alloc] init];
        photoGalleryVC.dataSource = self;
        photoGalleryVC.showStatusBar = YES;
    }
    
    if (self.navigationController)
        [self.navigationController pushViewController:photoGalleryVC animated:YES];
    else {
        photoGalleryVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentViewController:photoGalleryVC animated:YES completion:NULL];
    }
}

- (IBAction)segGalleryModeChanged:(UISegmentedControl *)sender {
    self.photoGallery.galleryMode = (UIPhotoGalleryMode)sender.selectedSegmentIndex;
    
    switch (sender.selectedSegmentIndex) {
        case UIPhotoGalleryModeImageLocal:
            self.photoGallery.subviewGap = 30;
            self.photoGallery.verticalGallery = NO;
            self.photoGallery.peakSubView = YES;
            break;
            
        case UIPhotoGalleryModeImageRemote:
            self.photoGallery.subviewGap = 30;
            self.photoGallery.verticalGallery = self.photoGallery.peakSubView = NO;
            break;
            
        case UIPhotoGalleryModeCustomView:
            self.photoGallery.subviewGap = 50;
            self.photoGallery.verticalGallery = self.photoGallery.peakSubView = YES;
            break;
            
        default:
            break;
    }
    
    [self.photoGallery layoutSubviews];
}



@end
